home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / SCIENTIF / H381.ZIP / GSRC208A.ZIP / GWTOP.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  26KB  |  704 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*          GWTOP - Topology         */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*           Topology window         */
  14. /*                                   */
  15. /*           QuickC/WIN 1.0          */
  16. /*                                   */
  17. /*   (include here compilers that    */
  18. /*   compiled GWSIM successfully)    */
  19. /*                                   */
  20. /*************************************/
  21.  
  22.  
  23. /*
  24.   be sure that IOTOP.C is recompiled and that
  25.   it iotop.obj from a GWSIM compilation is not
  26.   being used now. To be sure, delete iotop.obj
  27. */
  28.  
  29. /* this is needed for IOTOP.C */
  30. #define GWTOP
  31.  
  32. #include <windows.h>
  33. #include <string.h>
  34. #include "commdlg.h"
  35. #include "basic.h"                        /* basic windows fnction prototypes        */
  36. #include "defines.h"                    /* symbols also used in .DLG files        */
  37. #include "globals.h"                    /* gepasi's own symbols                    */
  38. #include "gwtop.h"                        /* macros, function prototypes, etc.    */
  39. #include "gep1.h"                        /* gepasi's variables                    */
  40. #include "strtbl.h"                        /* symbols for the string table            */
  41. #include "iotop.h"
  42.  
  43. #pragma alloc_text( CODE0, WinMain, WndProc, AboutBox, nCwRegisterClasses, CwUnRegisterClasses, CleanUp )
  44.  
  45. #define HELP_PARTIALKEY   0x0105
  46.  
  47.  
  48. /* Struct pointed to by WM_GETMINMAXINFO lParam
  49. typedef struct tagMINMAXINFO
  50. {
  51.     POINT ptReserved;
  52.     POINT ptMaxSize;
  53.     POINT ptMaxPosition;
  54.     POINT ptMinTrackSize;
  55.     POINT ptMaxTrackSize;
  56. } MINMAXINFO;
  57. */
  58.  
  59. /* global variables    */
  60. char szString[128];   /* variable to load resource strings         */
  61. char szHelpFile[] = "GEPASI.HLP";                    /* help file name                        */
  62. char szAppName[20];   /* class name for the window                    */
  63. int  lbWidth;         /* width of largest string in list box        */
  64. HWND            hInst;
  65. HWND            hWndMain;
  66. HCURSOR            hHourGlass;                /* handle to the wait cursor            */
  67. HMENU            hMenu;                    /* handle to the menu                    */
  68. GLOBALHANDLE    hMetn;                    /* handle to memory block w/ metn        */
  69. GLOBALHANDLE    hStepn;                    /* handle to memory block w/ stepn        */
  70. GLOBALHANDLE    hSto;                    /* handle to memory block w/ sto        */
  71. GLOBALHANDLE    hLp;                    /* handle to memory block w/ lp            */
  72. GLOBALHANDLE    hKinet;                    /* handle to memory block w/ kinet        */
  73. GLOBALHANDLE    hRs;                    /* handle to memory block w/ ord        */
  74. char            (huge *metn)[NAME_L];    /* pointer to work with metname array    */
  75. char            (huge *stepn)[NAME_L];    /* pointer to work with step names        */
  76. unsigned
  77.  char (huge *lp)[MAX_STEP][MAX_MET];    /* def. of modification loops            */
  78. int   (huge *rs)[MAX_STEP][MAX_MOL];    /* reaction structure                    */
  79. int                huge *sto;                /* pointer to work with metname array    */
  80. int                totm;                    /* number of total metabolites            */
  81. int                nst;                    /* number of steps                        */
  82. int                nlps;                    /* number of reactions with modifiers    */
  83. int                intm[MAX_MET];            /* 1 if internal metabolite                */
  84. unsigned char    rvr[MAX_STEP];            /* 1 if reaction is reversible            */
  85. int             huge *kinet;            /* pointer to mirror of kinetu            */
  86. unsigned char    nmd[MAX_STEP];            /* number of assigned modfs of a react.    */
  87. int                kinass, kass;            /* number of reactions w/ defined type    */
  88. int                loopass, lass;            /* number of reactions w/ defined modif.*/
  89. int             notsaved;                /* 1 if file has not been saved            */
  90. int                eqefl;                    /* 1 if editing existing u-d rate eq.    */
  91. char            FileName[256];            /* filename of file with full path        */
  92. char            FileTitle[13];            /* filename of file w/o full path        */
  93. char            errstr[80];                /* string to hold lex and parser errors    */
  94. unsigned char    lex;                    /* index for parser                        */
  95.  
  96. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  97. {
  98.  /***********************************************************************/
  99.  /* HANDLE hInstance;       handle for this instance                    */
  100.  /* HANDLE hPrevInstance;   handle for possible previous instances      */
  101.  /* LPSTR  lpszCmdLine;     long pointer to exec command line           */
  102.  /* int    nCmdShow;        Show code for main window display           */
  103.  /***********************************************************************/
  104.  
  105.  MSG        msg;           /* MSG structure to store your messages        */
  106.  int        nRc;           /* return value from Register Classes          */
  107.  HANDLE        hAcc;
  108.  LPSTR        ptr;
  109.  
  110.  strcpy(szAppName, "GWTOP");
  111.  hInst = hInstance;
  112.  if(!hPrevInstance)
  113.    {
  114.     /* register window classes if first instance of application         */
  115.     if ((nRc = nCwRegisterClasses()) == -1)
  116.       {
  117.        /* registering one of the windows failed                         */
  118.        LoadString(hInst, IDS_ERR_REGISTER_CLASS, szString, sizeof(szString));
  119.        MessageBeep( MB_ICONHAND );
  120.        MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  121.        return nRc;
  122.       }
  123.    }
  124.  
  125.  /* setup the device independent screen units    */
  126.  InitScrScale();
  127.  
  128.  /* load the wait cursor                        */
  129.  hHourGlass = LoadCursor(NULL, IDC_WAIT);
  130.  
  131.  /* load the accelerators table    */
  132.  hAcc = LoadAccelerators( hInst, (LPSTR) "ACCTABLE" );
  133.  
  134.  /* create application's Main window                                    */
  135.  hWndMain = CreateWindow(
  136.                 szAppName,               /* Window class name           */
  137.                 "GEPASI - Topology",     /* Window's title              */
  138.                 WS_CAPTION      |        /* Title and Min/Max           */
  139.                 WS_SYSMENU      |        /* Add system menu box         */
  140.                 WS_MINIMIZEBOX  |        /* Add minimize box            */
  141.                 WS_CLIPCHILDREN |        /* don't draw in child windows areas */
  142.                 WS_OVERLAPPED,
  143.                 CW_USEDEFAULT, 0,        /* Use default X, Y            */
  144.                 ScrX(150), ScrY(90),
  145.                 NULL,                    /* Parent window's handle      */
  146.                 NULL,                    /* Default to Class Menu       */
  147.                 hInst,                   /* Instance of window          */
  148.                 NULL);                   /* Create struct for WM_CREATE */
  149.  
  150.  
  151.  if(hWndMain == NULL)
  152.  {
  153.   LoadString(hInst, IDS_ERR_CREATE_WINDOW, szString, sizeof(szString));
  154.   MessageBeep( MB_ICONHAND );
  155.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  156.   return IDS_ERR_CREATE_WINDOW;
  157.  }
  158.  
  159.  if( InitGepasiVar() != 0 )                    /* initialize GEPASI's vars */
  160.  {
  161.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  162.   MessageBeep( MB_ICONHAND );
  163.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  164.   if(!hPrevInstance) CwUnRegisterClasses();
  165.   return IDS_ERR_OUT_OF_MEM;
  166.  }
  167.  /* lets allocate memory for mirror of metname    */
  168.  hMetn = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * NAME_L * sizeof( char ) );
  169.  if( hMetn == NULL )
  170.  {
  171.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  172.   MessageBeep( MB_ICONHAND );
  173.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  174.   CleanUp(hWndMain, hPrevInstance);
  175.   return IDS_ERR_OUT_OF_MEM;
  176.  }
  177.  hStepn = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * NAME_L * sizeof( char ) );
  178.  if( hStepn == NULL )
  179.  {
  180.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  181.   MessageBeep( MB_ICONHAND );
  182.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  183.   GlobalFree( hMetn );
  184.   CleanUp(hWndMain, hPrevInstance);
  185.   return IDS_ERR_OUT_OF_MEM;
  186.  }
  187.  /* lets allocate memory for mirror of stoiu    */
  188.  hSto = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_MET * MAX_STEP * sizeof( int ) );
  189.  if( hSto == NULL )
  190.  {
  191.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  192.   MessageBeep( MB_ICONHAND );
  193.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  194.   GlobalFree( hStepn );
  195.   GlobalFree( hMetn );
  196.   CleanUp(hWndMain, hPrevInstance);
  197.   return IDS_ERR_OUT_OF_MEM;
  198.  }
  199.  /* mirror of loop                                */
  200.  hLp = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * MAX_MET * sizeof( unsigned char ) );
  201.  if( hLp == NULL )
  202.  {
  203.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  204.   MessageBeep( MB_ICONHAND );
  205.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  206.   GlobalFree( hSto );
  207.   GlobalFree( hStepn );
  208.   GlobalFree( hMetn );
  209.   CleanUp(hWndMain, hPrevInstance);
  210.   return IDS_ERR_OUT_OF_MEM;
  211.  }
  212.  /* allocate memory for mirror of kinetu ...    */
  213.  hKinet = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * sizeof( int ) );
  214.  if( hKinet == NULL )
  215.  {
  216.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  217.   MessageBeep( MB_ICONHAND );
  218.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  219.   GlobalFree( hLp );
  220.   GlobalFree( hSto );
  221.   GlobalFree( hStepn );
  222.   GlobalFree( hMetn );
  223.   CleanUp(hWndMain, hPrevInstance);
  224.   return IDS_ERR_OUT_OF_MEM;
  225.  }
  226.  /* allocate memory for mirror of order ...        */
  227.  hRs = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, (DWORD) MAX_STEP * MAX_MOL * sizeof( int ) );
  228.  if( hRs == NULL )
  229.  {
  230.   LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  231.   MessageBeep( MB_ICONHAND );
  232.   MessageBox(NULL, szString, NULL, MB_ICONSTOP);
  233.   GlobalFree( hLp );
  234.   GlobalFree( hSto );
  235.   GlobalFree( hStepn );
  236.   GlobalFree( hMetn );
  237.   GlobalFree( hKinet );
  238.   CleanUp(hWndMain, hPrevInstance);
  239.   return IDS_ERR_OUT_OF_MEM;
  240.  }
  241.  
  242.  /* lock the memory blocks                        */
  243.  metn    = (char (huge *)[NAME_L])                        GlobalLock( hMetn );
  244.  stepn    = (char (huge *)[NAME_L])                        GlobalLock( hStepn );
  245.  sto    = (int huge *)                                    GlobalLock( hSto );
  246.  kinet    = (int huge *)                                     GlobalLock( hKinet );
  247.  lp        = (unsigned char (huge *)[MAX_STEP][MAX_MET])     GlobalLock( hLp );
  248.  rs        = (int (huge *)[MAX_STEP][MAX_MOL])                 GlobalLock( hRs );
  249.  
  250.  InitTxtMetrics( hWndMain );
  251.  ShowWindow(hWndMain, nCmdShow);            /* display main window      */
  252.  
  253.  /* if called with a .sim filename argument, store it in FileName    */
  254.  FileName[0] = '\0';
  255.  if( *lpszCmdLine)
  256.  {
  257.   ptr = _fstrchr( lpszCmdLine, ' ' );
  258.   if( ptr != NULL ) *ptr = '\0';
  259.   lstrcat( (LPSTR) FileName, lpszCmdLine );
  260.   SendMessage( hWndMain, WM_USER+1, 0, 0 );
  261.  }
  262.  
  263.  while(GetMessage(&msg, NULL, 0, 0))    /* Until WM_QUIT message    */
  264.  {
  265.   if( !TranslateAccelerator( hWndMain, hAcc, &msg ) )
  266.   {
  267.    TranslateMessage(&msg);
  268.    DispatchMessage(&msg);
  269.   }
  270.  }
  271.  
  272.  /* clean up before exiting from the application    */
  273.  GlobalUnlock( hSto );
  274.  GlobalUnlock( hLp );
  275.  GlobalUnlock( hMetn );
  276.  GlobalUnlock( hStepn );
  277.  GlobalUnlock( hKinet );
  278.  GlobalUnlock( hRs );
  279.  GlobalFree( hMetn );
  280.  GlobalFree( hStepn );
  281.  GlobalFree( hLp );
  282.  GlobalFree( hSto );
  283.  GlobalFree( hKinet );
  284.  GlobalFree( hRs );
  285.  CleanUp(hWndMain, hPrevInstance);
  286.  
  287.  return msg.wParam;
  288. } /*  End of WinMain                                                    */
  289.  
  290. /************************************************************************/
  291. /*                                                                      */
  292. /* Main Window Procedure                                                */
  293. /*                                                                      */
  294. /* This procedure provides service routines for the Windows events      */
  295. /* (messages) that Windows sends to the window, as well as the user     */
  296. /* initiated events (messages) that are generated when the user selects */
  297. /* the action bar and pulldown menu controls or the corresponding       */
  298. /* keyboard accelerators.                                               */
  299. /*                                                                      */
  300. /************************************************************************/
  301.  
  302. LONG FAR PASCAL WndProc(HWND hWnd, WORD Message, WORD wParam, LONG lParam)
  303. {
  304.  HBITMAP    hBitmap=0;          /* handle for bitmaps                    */
  305.  HDC        hDC;                /* handle for the display device        */
  306.  MINMAXINFO FAR* lpmmi;            /* pointer to minmaxinfo structure        */
  307.  PAINTSTRUCT    ps;             /* holds PAINT information                */
  308.  int            nRc=0;          /* return code                            */
  309.  FARPROC        lpfnProc;        /* pointer to dialog procedures            */
  310.  char            szRslt[128];
  311.  char            szASTR[128];
  312.  int            i, j;
  313.  
  314.  switch (Message)
  315.  {
  316.   case WM_CREATE:
  317.    /* get handle to the menu                                    */
  318.    hMenu = GetMenu( hWnd );
  319.    SendMessage( hWnd, WM_COMMAND, IDM_F_NEW, 0 );
  320.    break;       /*  End of WM_CREATE                                    */
  321.  
  322.   case WM_CLOSE:  /* close the window                                 */
  323.    if( notsaved )
  324.     AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNO );
  325.    if( newtree ) out_tree();
  326.    WinHelp( hWnd, (LPSTR) szHelpFile, HELP_QUIT, 0 );
  327.    DestroyWindow(hWnd);
  328.    if (hWnd == hWndMain)
  329.     PostQuitMessage(0);  /* Quit the application                 */
  330.    break;
  331.  
  332.   case WM_QUERYENDSESSION:  /* close the window                                 */
  333.    if( notsaved )
  334.    {
  335.     nRc = AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNOCANCEL );
  336.     if ( nRc != IDCANCEL ) return 1;
  337.     else return 0;
  338.    }
  339.    return 1;
  340.  
  341.   case WM_COMPACTING:
  342.    LoadString(hInst, IDS_ERR_OUT_OF_MEM, szString, sizeof(szString));
  343.    MessageBeep( MB_ICONEXCLAMATION );
  344.    MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
  345.    return 1;
  346.  
  347.   case WM_SIZE:     /*  code for sizing client area                   */
  348.    switch( wParam )
  349.    {
  350.     case SIZEICONIC:
  351.      if( FileTitle[0] != '\0' )
  352.       wsprintf( (LPSTR) szASTR, "Topology - %s", (LPSTR) FileTitle );
  353.      else
  354.       wsprintf( (LPSTR) szASTR, "GEPASI - Topology" );
  355.      break;
  356.     case SIZENORMAL:
  357.     case SIZEFULLSCREEN:
  358.      wsprintf( (LPSTR) szASTR, "GEPASI - Topology" );
  359.     break;
  360.    }
  361.    SendMessage( hWnd, WM_SETTEXT, 0, (DWORD) (LPSTR) szASTR );
  362.    break;       /* End of WM_SIZE                                 */
  363.  
  364.   case WM_PAINT:    /* code for the window's client area          */
  365.    /* Obtain a handle to the device context                       */
  366.    /* BeginPaint will sends WM_ERASEBKGND if appropriate          */
  367.    memset( &ps, 0x00, sizeof( PAINTSTRUCT ) );
  368.    hDC = BeginPaint(hWnd, &ps);
  369.    /* Included in case the background is not a pure color         */
  370.    SetBkMode( hDC, TRANSPARENT );
  371.    i=0;
  372.    if( FileName[0] != '\0' )
  373.     wsprintf( szASTR, "filename: %s", (LPSTR) FileTitle );
  374.    else
  375.     wsprintf( szASTR, "undefined filename");
  376.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  377.    wsprintf( szASTR, "%i metabolites", totmet );
  378.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  379.    wsprintf( szASTR, "%i steps", nsteps );
  380.    TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  381.    if( (nsteps > 0) && (kinass != nsteps) )
  382.    {
  383.     wsprintf( szASTR, "%i kinetics undefined", nsteps - kinass );
  384.     TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  385.    }
  386.    if( (nloops > 0) && (loopass != nloops) )
  387.    {
  388.     wsprintf( szASTR, "%i loops undefined", nloops - loopass );
  389.     TextOut( hDC, TxtX(0), TxtY(i++), szASTR, strlen( szASTR ) );
  390.    }
  391.    EndPaint( hWnd, &ps );
  392.    break;       /*  End of WM_PAINT                               */
  393.  
  394.   case WM_USER+1:
  395.    if( !(nRc = ReadTop( FileName ) ) )
  396.    {
  397.     /* get the filename without the path                        */
  398.     GetFileTitle( (LPSTR) FileName, (LPSTR) FileTitle, sizeof(FileTitle) );
  399.     /* signal that it has not yet been changed                */
  400.     notsaved = 0;
  401.     /* count the number of loops and assigned kinetic types    */
  402.     for( i=0, nloops=0, kinass=0, loopass=0; i<nsteps; i++ )
  403.     {
  404.      /* count the number of assigned kinetics                */
  405.      if( kinetu[i] != NOT ) kinass++;
  406.      /* count the number of loops                            */
  407.      if( ktype[kinetu[i]].nmodf > 0 ) nloops++;
  408.      /* count the number of assigned modfs for each reaction    */
  409.      for( j=0, nmod[i]=0; j<totmet; j++ )
  410.       if( (*loop)[i][j] != 0 ) nmod[i]++;
  411.      /* count the number of assigned loops                    */
  412.      if( (ktype[kinetu[i]].nmodf>0) &&
  413.          (ktype[kinetu[i]].nmodf == nmod[i]) ) loopass++;
  414.     }
  415.     if( nsteps>0 )
  416.     {
  417.      EnableMenuItem( hMenu, IDM_E_KINETICS , MF_ENABLED );
  418.      EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_ENABLED );
  419.     }
  420.     else
  421.     {
  422.      EnableMenuItem( hMenu, IDM_E_KINETICS , MF_GRAYED );
  423.      EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_GRAYED );
  424.      EnableMenuItem( hMenu, IDM_E_LOOPS , MF_GRAYED );
  425.     }
  426.     EnableMenuItem( hMenu, IDM_F_SAVE, MF_ENABLED );
  427.     if( nloops > 0 )
  428.      EnableMenuItem( hMenu, IDM_E_LOOPS , MF_ENABLED );
  429.     else
  430.      EnableMenuItem( hMenu, IDM_E_LOOPS , MF_GRAYED );
  431.     InvalidateRect( hWnd, NULL, TRUE );
  432.    }
  433.    else
  434.    {
  435.     LoadString( hInst, nRc, szString, sizeof(szString) );
  436.     MessageBeep( MB_ICONQUESTION );
  437.     MessageBox( NULL, szString, NULL, MB_ICONINFORMATION );
  438.     SendMessage( hWnd, WM_COMMAND, IDM_F_NEW, 0 );
  439.    }
  440.    break;
  441.  
  442.   case WM_COMMAND:
  443.    switch (wParam)
  444.    {
  445.     case IDA_F1HELP:
  446.     case IDM_H_CONTENTS:
  447.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_INDEX, 0 );
  448.         break;
  449.  
  450.     case IDM_H_SEARCH:
  451.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_PARTIALKEY, (DWORD) (LPSTR) "" );
  452.         break;
  453.  
  454.     case IDM_H_HELPONHELP:
  455.         WinHelp( hWnd, (LPSTR) szHelpFile, HELP_HELPONHELP, 0 );
  456.         break;
  457.  
  458.     case IDM_H_ABOUT:
  459.      lpfnProc = MakeProcInstance((FARPROC)AboutBox, hInst);
  460.      DialogBox(hInst, (LPSTR)"ABOUTBOX", hWnd, lpfnProc);
  461.      FreeProcInstance(lpfnProc);
  462.      break;
  463.  
  464.     case IDM_F_OPEN:
  465.      if( notsaved )
  466.       AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNO );
  467.      if( GetOFileName( hWnd,
  468.                          (LPSTR) FileName, 256,
  469.                         (LPSTR) "Open topology file",
  470.                          (LPSTR) "topology (*.TOP)\0*.TOP\0simulation (*.SIM)\0*.SIM\0" ) )
  471.       SendMessage( hWnd, WM_USER+1, 0, 0 );
  472.      break;
  473.  
  474.     case IDM_F_CLOSE:
  475.      SendMessage( hWnd, WM_CLOSE, 0, 0 );
  476.      break;
  477.  
  478.     case IDM_F_SAVE:
  479.      WriteTop( FileName );
  480.      notsaved = 0;
  481.      break;
  482.  
  483.     case IDM_F_SAVEAS:
  484.      if( GetSFileName( hWnd,
  485.                          (LPSTR) FileName, sizeof(FileName),
  486.                         (LPSTR) "Save topology file",
  487.                          (LPSTR) "topology (*.TOP)\0*.TOP\0" ) )
  488.      {
  489.       if( !(nRc = WriteTop( FileName ) ) )
  490.       {
  491.        /* get the filename without the path                        */
  492.        GetFileTitle( (LPSTR) FileName, (LPSTR) FileTitle, sizeof(FileTitle) );
  493.        /* signal that there has not been any change yet            */
  494.        notsaved = 0;
  495.        EnableMenuItem( hMenu, IDM_F_SAVE, MF_ENABLED );
  496.        InvalidateRect( hWnd, NULL, TRUE );
  497.       }
  498.      }
  499.      break;
  500.  
  501.     case IDM_F_NEW:
  502.      if( notsaved )
  503.       AskAboutSave( hWnd, hInst, FileName, MB_ICONQUESTION | MB_YESNO );
  504.      for( i=0; i<MAX_STEP; i++ ) kinetu[i] = NOT;
  505.      nloops = kinass = loopass = notsaved = 0;
  506.      topname[0] = '\0';
  507.      FileName[0] = '\0';
  508.      ClearReactions();
  509.      EnableMenuItem( hMenu, IDM_F_SAVE, MF_GRAYED );
  510.      EnableMenuItem( hMenu, IDM_E_KINETICS, MF_GRAYED );
  511.      EnableMenuItem( hMenu, IDM_E_METABOLITES, MF_GRAYED );
  512.      EnableMenuItem( hMenu, IDM_E_LOOPS, MF_GRAYED );
  513.      InvalidateRect( hWnd, NULL, TRUE );
  514.      break;
  515.  
  516.     case IDM_E_REACTIONS:
  517.      lpfnProc = MakeProcInstance( (FARPROC)EdReact, hInst);
  518.      DialogBox( hInst, (LPSTR)"TOP_PARSER", hWnd, lpfnProc );
  519.      FreeProcInstance( lpfnProc );
  520.      hMenu = GetMenu( hWnd );
  521.      if( nsteps>0 )
  522.      {
  523.       EnableMenuItem( hMenu, IDM_E_KINETICS , MF_ENABLED );
  524.       EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_ENABLED );
  525.      }
  526.      else
  527.      {
  528.       EnableMenuItem( hMenu, IDM_E_KINETICS , MF_GRAYED );
  529.       EnableMenuItem( hMenu, IDM_E_METABOLITES , MF_GRAYED );
  530.       EnableMenuItem( hMenu, IDM_E_LOOPS , MF_GRAYED );
  531.      }
  532.      InvalidateRect( hWnd, NULL, TRUE );
  533.      break;
  534.  
  535.     case IDM_E_METABOLITES:
  536.      lpfnProc = MakeProcInstance((FARPROC)EdMetnam, hInst);
  537.      DialogBox(hInst, (LPSTR)"METNAM", hWnd, lpfnProc);
  538.      FreeProcInstance(lpfnProc);
  539.      InvalidateRect( hWnd, NULL, TRUE );
  540.      break;
  541.  
  542.     case IDM_E_KINETICS:
  543.      lpfnProc = MakeProcInstance((FARPROC) EdKinet, hInst);
  544.      DialogBox(hInst, (LPSTR)"REACT_KINET", hWnd, lpfnProc);
  545.      FreeProcInstance(lpfnProc);
  546.      if( nloops > 0 )
  547.       EnableMenuItem( hMenu, IDM_E_LOOPS , MF_ENABLED );
  548.      InvalidateRect( hWnd, NULL, TRUE );
  549.      break;
  550.  
  551.     case IDM_E_LOOPS:
  552.      lpfnProc = MakeProcInstance((FARPROC) EdLoop, hInst);
  553.      DialogBox(hInst, (LPSTR)"LOOP_ED", hWnd, lpfnProc);
  554.      FreeProcInstance(lpfnProc);
  555.      InvalidateRect( hWnd, NULL, TRUE );
  556.      break;
  557.  
  558.     case IDM_E_NAME:
  559.      lpfnProc = MakeProcInstance((FARPROC) EdName, hInst);
  560.      DialogBox(hInst, (LPSTR)"TITLE_ED", hWnd, lpfnProc);
  561.      FreeProcInstance(lpfnProc);
  562.      InvalidateRect( hWnd, NULL, TRUE );
  563.      break;
  564.  
  565.     case IDM_E_RATES:
  566.      lpfnProc = MakeProcInstance((FARPROC) EdUdkt, hInst);
  567.      DialogBox(hInst, (LPSTR)"ED_UDKT", hWnd, lpfnProc);
  568.      FreeProcInstance(lpfnProc);
  569.      InvalidateRect( hWnd, NULL, TRUE );
  570.      break;
  571.  
  572.     default:
  573.      return DefWindowProc(hWnd, Message, wParam, lParam);
  574.    }
  575.    break;        /* End of WM_COMMAND                             */
  576.  
  577.   default:
  578.    return DefWindowProc(hWnd, Message, wParam, lParam);
  579.  }
  580.  return 0L;
  581. }     /* End of WndProc                                         */
  582.  
  583.  
  584. BOOL FAR PASCAL AboutBox(HWND hWndDlg, WORD Message, WORD wParam, LONG lParam)
  585. {
  586.  switch(Message)
  587.  {
  588.   case WM_INITDIALOG:
  589.    SendDlgItemMessage( hWndDlg, IDSTAT_2, WM_SETTEXT, 0, (DWORD) (LPSTR) VERSION );
  590.    break;
  591.  
  592.   case WM_CLOSE:
  593.    /* Closing the Dialog behaves the same as Cancel               */
  594.    PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
  595.    break;
  596.  
  597.   case WM_COMMAND:
  598.    if( wParam == IDOK || wParam == IDCANCEL )
  599.    EndDialog(hWndDlg, FALSE);
  600.    break;
  601.  
  602.   default:
  603.    return FALSE;
  604.  }
  605.  return TRUE;
  606. }
  607.  
  608.  
  609. /************************************************************************/
  610. /*                                                                      */
  611. /* nCwRegisterClasses Function                                          */
  612. /*                                                                      */
  613. /* The following function registers all the classes of all the windows  */
  614. /* associated with this application. The function returns an error code */
  615. /* if unsuccessful, otherwise it returns 0.                             */
  616. /*                                                                      */
  617. /************************************************************************/
  618.  
  619. int nCwRegisterClasses(void)
  620. {
  621.  WNDCLASS   wndclass;    /* struct to define a window class             */
  622.  
  623.  memset(&wndclass, 0x00, sizeof(WNDCLASS));
  624.  /* load WNDCLASS with window's characteristics                         */
  625.  wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW;
  626.  wndclass.lpfnWndProc = WndProc;
  627.  /* Extra storage for Class and Window objects                          */
  628.  wndclass.cbClsExtra = 0;
  629.  wndclass.cbWndExtra = 0;
  630.  wndclass.hInstance = hInst;
  631.  wndclass.hIcon = LoadIcon(hInst,"GWTOP");
  632.  wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  633.  /* Create brush for erasing background                                 */
  634.  wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  635.  wndclass.lpszMenuName = szAppName;   /* Menu Name is App Name */
  636.  wndclass.lpszClassName = szAppName; /* Class Name is App Name */
  637.  if(!RegisterClass(&wndclass))
  638.    return -1;
  639.  return(0);
  640. } /* End of nCwRegisterClasses                                          */
  641.  
  642. /************************************************************************/
  643. /*  cwCenter Function                                                   */
  644. /*                                                                      */
  645. /*  centers a window based on the client area of its parent             */
  646. /*                                                                      */
  647. /************************************************************************/
  648.  
  649. void cwCenter( HWND hWnd, int top)
  650. {
  651.  POINT      pt;
  652.  RECT       swp;
  653.  RECT       rParent;
  654.  int        iwidth;
  655.  int        iheight;
  656.  
  657.  /* get the rectangles for the parent and the child                     */
  658.  GetWindowRect(hWnd, &swp);
  659.  GetClientRect(hWndMain, &rParent);
  660.  
  661.  /* calculate the height and width for MoveWindow                       */
  662.  iwidth = swp.right - swp.left;
  663.  iheight = swp.bottom - swp.top;
  664.  
  665.  /* find the center point and convert to screen coordinates             */
  666.  pt.x = (rParent.right - rParent.left) / 2;
  667.  pt.y = (rParent.bottom - rParent.top) / 2;
  668.  ClientToScreen(hWndMain, &pt);
  669.  
  670.  /* calculate the new x, y starting point                               */
  671.  pt.x = pt.x - (iwidth / 2);
  672.  pt.y = pt.y - (iheight / 2);
  673.  
  674.  /* top will adjust the window position, up or down                     */
  675.  if(top)
  676.    pt.y = pt.y + top;
  677.  
  678.  /* move the window                                                     */
  679.  MoveWindow(hWnd, pt.x, pt.y, iwidth, iheight, FALSE);
  680. }
  681.  
  682. /************************************************************************/
  683. /*  CwUnRegisterClasses Function                                        */
  684. /*                                                                      */
  685. /*  Deletes any refrences to windows resources created for this         */
  686. /*  application, frees memory, deletes instance, handles and does       */
  687. /*  clean up prior to exiting the window                                */
  688. /*                                                                      */
  689. /************************************************************************/
  690.  
  691. void CwUnRegisterClasses(void)
  692. {
  693. /* WNDCLASS   wndclass;
  694.  memset(&wndclass, 0x00, sizeof(WNDCLASS));*/
  695.  
  696.  UnregisterClass(szAppName, hInst);
  697. }    /* End of CwUnRegisterClasses                                      */
  698.  
  699.  
  700. void CleanUp( HWND hWnd, HANDLE hPrevInstance )
  701. {
  702.  if(!hPrevInstance) CwUnRegisterClasses();
  703.  TidyGepasiVar();
  704. }